#!/usr/bin/perl

###################################################################
# ARG LIST
#
# 1 => freq
# 2 => data dir, from clean 
# 3 => length (OPTIONAL used for ouput info)
# 4 => start  (OPTIONAL used for ouput info)
# 5 => end    (OPTIONAL used for ouput info)
###################################################################

use Math::Trig; #for acos()

$PI = 3.14159265358979323846264;
$freq = $ARGV[0];
print STDERR "\n\n";
print STDERR "E> ===============================================\n";
print STDERR "E> Freq = $freq\n";
$datadir = $ARGV[1];
$cleanfile = "${datadir}cleanSpect.dat";
$paramfile = "${datadir}param.dat";
print STDERR "E> Data = $cleanfile\n";
print STDERR "E>      = $paramfile\n";
$length = $ARGV[2];
$start = $ARGV[3];
$end = $ARGV[4];
print STDERR "E> -----------------------------------------------\n";

open(CFILE,"$cleanfile");
@spect = <CFILE>;
close(CFILE);

open(PFILE,"$paramfile") || die "parameter file not found!";
@params = <PFILE>;
@params = split(/ /,$params[0]);
$PmeanTime = $params[0];
$PstartTime = $params[1];
$PendTime = $params[2] * 1.0;
print STDERR "E> params meanTime  = $PmeanTime\n";
print STDERR "E> params startTime = $PstartTime\n";
print STDERR "E> params endTime = $PendTime\n";

$small = 9E9;
$big = -9E9;
$i=0;
foreach (@spect) {
    split;
    if ($_[0] <= $freq) {
	$small = $_[0];
	$i++;
    }
}
@big = split(/ /,$spect[$i]);
$Ismall = $i-1;
$Ibig = $i;
$big = $big[0];
$bigdiff = $big-$freq;
$smalldiff = $freq-$small;
print STDERR "E> Lower freq. (i, value, diff) <||> Upper freq. (i, value, diff)\n";
print STDERR "E> $Ismall, $small, $smalldiff <||> $Ibig, $big, $bigdiff\n";

if ($smalldiff < $bigdiff) {
    $Iphase = $Ismall;
    print STDERR "E> Using the lower freq, $Iphase\n";
} else {
    $Iphase = $Ibig;
    print STDERR "E> Using the upper freq, $Iphase\n";
}

@phase = split(/ /,$spect[$Iphase]);
$nearfreq = $phase[0];
$phase = $phase[4];
$phasePI = $phase/$PI;
$nearIM = $phase[2];
$nearRE = $phase[1];
print STDERR "E> freq = $nearfreq\n";
print STDERR "E> Phase found = $phase\n";
print STDERR "E> Phase / pi  = $phasePI\n";
print STDERR "E> ===============================================\n";

@bigphase = split(/ /,$spect[$Ibig]);
$bigphase = $bigphase[4];
$ReBig = $bigphase[1];
$ImBig = $bigphase[2];
$bfreq = $bigphase[0];

@smallphase = split(/ /,$spect[$Ismall]);     
$smallphase = $smallphase[4];
$sfreq = $smallphase[0];
$ReSmall = $smallphase[1];
$ImSmall = $smallphase[2];

$avefreq = ($bfreq + $sfreq)/2.0;
$aveRE = ($ReBig + $ReSmall)/2.0;
$aveIM = ($ImBig + $ImSmall)/2.0;

$avephase = ($bigphase+$smallphase)/2.0;
$avephasePI = $avephase/$PI;

# check to see if closest point is neared to the desired freq than ave
if ( abs($avefreq - $freq) > abs($nearfreq - $freq) ) {
    $FREQ = $nearfreq;
    $PHASE = $phase;
    $PHASEPI = $phasePI;
    $IM = $nearIM;
    $RE = $nearRE;
} else {
    $FREQ = $avefreq;
    $PHASE = $avephase;
    $PHASEPI = $avephasePI;
    $IM = $aveIM;
    $RE = $aveRE;
}

$CORRECT = acos(cos(2*$PI*$freq*$PmeanTime));
if ($IM > 0) { $CORRECT = -1.0*$CORRECT; }
$CPHASE = $PHASE+$CORRECT;
$CPHASEPI = $CPHASE/$PI;
$PHASEDEG = $PHASE/($PI)*180.0;

#print STDOUT "$freq, $length, $start, $PstartTime, $end, ";
#print STDOUT "$PendTime, $PmeanTime, $avefreq, $avephase, $avephasePI, ";
#print STDOUT "$aveRE, $aveIM, $avephase2, $avephasePI2, $correction, $CPHASE, $CPHASEPI\n";
#print STDOUT "(${freq}) $FREQ R(${RE}) I(${IM}) (${PmeanTime}) [${PHASE}+${CORRECT}]=${CPHASE} <${CPHASEPI}>\n"
print STDOUT "(${freq}) $FREQ R(${RE}) I(${IM}) (${PmeanTime}) ${PHASE} <${PHASEPI}> <${PHASEDEG}>\n"


